import danmaku from './danmaku';
import utils from '../ass/utils';
import logger from '../core/logger';
import { Duration } from '@ohos/danmakuflamemaster';
import { Danmakus } from '@ohos/danmakuflamemaster';
import { IDisplayer } from '@ohos/danmakuflamemaster';
import { BaseDanmaku } from '@ohos/danmakuflamemaster';
import { DanmakuUtils } from '@ohos/danmakuflamemaster';
import { SpecialDanmaku } from '@ohos/danmakuflamemaster';
import { DanmakuFactory } from '@ohos/danmakuflamemaster';
import { BaseDanmakuParser } from '@ohos/danmakuflamemaster';
import { IDanmakus, ST_BY_TIME } from '@ohos/danmakuflamemaster';

export class biliDanmakuParser extends BaseDanmakuParser {
  protected mDispScaleX: number;
  protected mDispScaleY: number;

  parse(): IDanmakus {
    let result: Danmakus;
    try {
      result = new Danmakus({
        sortType: ST_BY_TIME,
        duplicateMergingEnabled: false,
        baseComparator: this.mContext.getBaseComparator()
      });
      let danmuFontSize: string = AppStorage.Get("danmuFontSize") == undefined ? "1.0X" : AppStorage.Get("danmuFontSize");
      let fontSizeScale: number = Number.parseFloat(danmuFontSize);
      if (this.mDataSource != null) {
        this.mDataSource.data().forEach((sourceItem: danmaku) => {
          let item = this.mContext.mDanmakuFactory.createDanmakuByContext(Number(sourceItem.danmakuType), this.mContext)
          if (item != null) {
            item.setTime(sourceItem.time);
            item.textSize = sourceItem.textSize * fontSizeScale;
            item.textColor = sourceItem.textColor;
            item.textShadowColor = sourceItem.textShadowColor;
            item.text = sourceItem.text;
            item.index = sourceItem.index;
            DanmakuUtils.fillText(item, item.text);
            if (Number(sourceItem.danmakuType) == BaseDanmaku.TYPE_SPECIAL) {
              item.duration = new Duration(sourceItem.duration);
              item.rotationZ = sourceItem.rotationZ;
              item.rotationY = sourceItem.rotationY;
              this.mContext.mDanmakuFactory.fillTranslationData(item, sourceItem.beginX, sourceItem.beginY,
                sourceItem.endX, sourceItem.endY, sourceItem.translationDuration, 0, this.mDispScaleX, this.mDispScaleY);
              this.mContext.mDanmakuFactory.fillAlphaData(item,
                sourceItem.beginAlpha, sourceItem.endAlpha, sourceItem.alphaDuration);
              (<SpecialDanmaku> item).isQuadraticEaseOut = false;
            }
          }
          if (!utils.isEmpty(item?.text)) {
            if (item.duration != null) {
              item.setTimer(this.mTimer);
              item.flags = this.mContext.mGlobalFlagValues;
              result.addItem(item);
            }
          }
        });
      }
    } catch (e) {
      logger.error(e);
    }
    return result;
  }

  setDisplayer(display: IDisplayer) {
    try {
      super.setDisplayer(display);
      this.mDispScaleX = this.mDispWidth / DanmakuFactory.BILI_PLAYER_WIDTH;
      this.mDispScaleY = this.mDispHeight / DanmakuFactory.BILI_PLAYER_HEIGHT;
    } catch (e) {
      logger.error(e);
    }
    return this;
  }
}